package ServerClient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client {
	
	public static final int PORT = 8988;
	
	public static void main(String[] args) {
		
		try {
			InetAddress addr = InetAddress.getByName("127.0.0.1");
			Socket sock = new Socket(addr, PORT);
			
			BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
			
			//command prompt
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			
			String command = "";
			String username = "";
			
			while(!command.equalsIgnoreCase("QUIT")){
				if(username.equals("")){
					System.out.print("Prijavljivanje: ");
					
					command = br.readLine();
					
					if(command.toUpperCase().startsWith("LOGIN ") && command.length() > 6) {
						System.out.println("[Client]: " + command);
						out.println(command);
						
						String response = in.readLine();
						System.out.println("[Server]: "+response);
						
						if(response.equals("Uspesno ste se prijavili"))
							username = command.substring(6);
					
						System.out.println("");
					} else if(command.toUpperCase().startsWith("REGISTER ") && command.length() > 9){
						System.out.println("[Client]: " + command);
						out.println(command);
						
						String response = in.readLine();
						System.out.println("[Server]: "+response);
						
						if(response.equals("Uspesno ste se registrovali"))
							username = command.substring(9);

						System.out.println("");
					} else {
						System.out.println("Morate se prijaviti!");
					}
					
					System.out.println("");
					continue;
				} else {
					System.out.println("Prijavljen korisnik: " + username);
					System.out.print("Unesite zeljenu komandu: " );
				}
				command = br.readLine();
			        
				if(command.equalsIgnoreCase("LOGOFF")){ 
					System.out.println("Uspesno ste se odjavili!");

					System.out.println("");
					
					username = "";
					
				} else if(command.toUpperCase().startsWith("ADD ") && command.length() > 4){
				
					System.out.println("[Client]: " + command);
					out.println(command);
						
					String response = in.readLine();
					System.out.println("[Server]: "+response);

					if(response.equals("Neispravni podaci")){
						System.out.println("");
						System.out.println("Proverite podatke");
						System.out.println("Podaci moraju biti u obliku: IdLeta   DatumPolaska       DatumDolaska       PolazniAerodrom   Odrediste   BrojPutnika");
						System.out.println("Primer:                      AAAAAA   12.12.2012_15:15   12.12.2012_16:30   Beograd           London      200");
					} else if(response.equals("Neispravni datum")){
						System.out.println("");
						System.out.println("Proverite datum");
						System.out.println("Datum mora biti u obliku: dd.mm.yyyy_HH:mm");
						System.out.println("Primer:                   12.12.2012_15:15");
					}
					
					System.out.println("");
					
				} else if(command.toUpperCase().startsWith("LIST ") && command.length() > 5){
				
					System.out.println("[Client]: " + command);
					out.println(command);
						
					String response = in.readLine();
					System.out.println("[Server]: "+response);

					System.out.println("");
				} else if(command.equalsIgnoreCase("LIST_ALL")){
				
					System.out.println("[Client]: " + command);
					out.println(command);
	
					String response;
					
					String list = "";
					int i=1;
					while (!(response = in.readLine()).equals("")) {
						list += i++ + ". " + response + "\n   ";
					}

					System.out.println("[Server]:\n   " + list);

					System.out.println("");
				} else if (command.toUpperCase().startsWith("RESERVE ") && command.length() > 8) {
					
					System.out.println("[Client]: " + command);
					out.println(command);
						
					String response = in.readLine();
					System.out.println("[Server]: "+response);

					System.out.println("");
					
					if(response.equals("Neispravni podaci")){
						System.out.println("Proverite podatke");
						System.out.println("Podaci moraju biti u obliku: IdLeta   JMBG           Ime     Prezime");
						System.out.println("Primer:                      AAAAAA   1234567890123  Petar   Petrovic");
					}
					
				} else if (command.toUpperCase().startsWith("CANCEL ") && command.length() > 7) {
					
					System.out.println("[Client]: " + command);
					out.println(command);
						
					String response = in.readLine();
					System.out.println("[Server]: "+response);

					System.out.println("");
					
					if(response.equals("Neispravni podaci")){
						System.out.println("Proverite podatke");
						System.out.println("Podaci moraju biti u obliku: IdLeta   JMBG");
						System.out.println("Primer:                      AAAAAA   1234567890123");
					}
					
					
				} else if (command.toUpperCase().startsWith("LIST_PASSENGER ") && command.length() > 15) {
					
					System.out.println("[Client]: " + command);
					out.println(command);
						
					String response;
					
					String list = "";
					int i=1;
					while (!(response = in.readLine()).equals("")) {
						list += i++ + ". " + response + "\n   ";
					}

					System.out.println("[Server]:\n   " + list);

					System.out.println("");
					
				} else if(command.equalsIgnoreCase("QUIT")){
					break;
				} else {
					System.out.println("Nepoznata komanda, pokusajte ponovo (LOGIN, REGISTER, LOGOFF, ADD, LIST, LIST_ALL, RESERVE, CANCEL, LIST_PASSENGER)");
					System.out.println("");
				}
			}
			
			System.out.println("[Client]: " + command);
			out.println(command);
				
			String response = in.readLine();
			System.out.println("[Server]: "+ response);

			System.out.println("");
			
			System.out.println("PREKID RADA");
			
			
			in.close();
			out.close();
			sock.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}